package com.huadao.pda.common;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.huadao.pda.main.PdaApplication;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class UtilHelpers {

    private String TAG="UtilHelpers";

    private static final UtilHelpers ourInstance = new UtilHelpers();

    //为防止X轴的日期数值太大,减去这个值.
    public static final long referenceTime=new Date().getTime();

    public static UtilHelpers getInstance() {
        return ourInstance;
    }

    private UtilHelpers() {
    }

    //检测IP格式
    public static boolean validIp(String ip){
        String expression = "^(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|[1-9])\\."

                + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."

                + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)\\."

                + "(1\\d{2}|2[0-4]\\d|25[0-5]|[1-9]\\d|\\d)$";

        Pattern p=Pattern.compile(expression);
        Matcher m=p.matcher(ip);
        return m.matches();
    }

    public static float getTime(Date date) {
        return date.getTime()-referenceTime;
    }

    public static int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public static boolean isServiceRunning(Context context, String ServiceName){
        if (TextUtils.isEmpty(ServiceName)) {
            return false;
        }
        ActivityManager myManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        ArrayList<ActivityManager.RunningServiceInfo> runningService = (ArrayList<ActivityManager.RunningServiceInfo>) myManager
                .getRunningServices(Integer.MAX_VALUE);
        for (int i = 0; i < runningService.size(); i++) {
            if (runningService.get(i).service.getClassName().equals(ServiceName)) {
                return true;
            }
        }
        return false;
    }

    //获取本机MAC值,没有获取到则返回null
    public static String getMAC(){
        try {
            List<NetworkInterface> all= Collections.list(NetworkInterface.getNetworkInterfaces());
            for(NetworkInterface nif:all){
                if(nif.getName().equalsIgnoreCase("wlan0")){
                    continue;
                }

                byte[] macBytes=nif.getHardwareAddress();
                if(macBytes==null){
                    return null;
                }

                StringBuilder res=new StringBuilder();
                for(byte b:macBytes){
                    res.append(String.format("%02x:",b));
                }

                if(res.length()>0){
                    res.deleteCharAt(res.length()-1);
                }
                return res.toString();
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Android 6.0 之前（不包括6.0）获取mac地址
     * 必须的权限 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
     * @param context * @return
     */
    public static String getMacDefault(Context context) {
        String mac = "";
        if (context == null) {
            return mac;
        }
//        WifiManager wifi = (WifiManager)PdaApplication.getInstance().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        WifiManager wifi = (WifiManager)context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = null;
        try {
            info = wifi.getConnectionInfo();
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (info == null) {
            return null;
        }
        mac = info.getMacAddress();
        if (!TextUtils.isEmpty(mac)) {
            mac = mac.toUpperCase(Locale.ENGLISH);
        }
        return mac;
    }

    public static int getStateBar(){
        int result = 0;
        int resourceId = PdaApplication.getInstance().getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = PdaApplication.getInstance().getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
}
